<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Db;

class Driver Extends Base
{
    protected $id = 'driver_id';
	protected $table = 'driver';
	protected $join = ['car_rental cr','cr.car_rental_id = m.car_rental_id','left'];
	protected $field = 'm.*,cr.name AS car_rental_name';
	
	
	//绑定
	public function bind() {
		
		$this->info();
		if($this->bind) {
			if($this->bind['delete_time']) {
				return $this->error('此车已经绑定过');
			} else {
				$rslt = Db::name('DriverCar')->where('driver_car_id',$this->bind['driver_car_id'])->setField('delete_time',NULL);
			}
			
		} else {
			$rslt = Db::name('DriverCar')->insert($this->relation);
		}
		
		return $this->res($rslt);
	}
	//解除绑定
	public function unbind() {
		
		$this->info();
		if($this->bind) {
			if($this->bind['delete_time']) {
				return $this->error('此车未绑定');
			} else {
				$rslt = Db::name('DriverCar')->where('driver_car_id',$this->bind['driver_car_id'])->setField('delete_time',time());
			}
			
		} else {
			return $this->error('此车未绑定');
		}
		
		return $this->res($rslt);
	}
	
	protected function info() {
		$driver_id = input('post.driver_id');
		$car_id = input('post.car_id');
		
		$this->relation = ['driver_id'=>$driver_id,'car_id'=>$car_id];
		
		//是否已经绑定
		$this->bind = Db::name('DriverCar')->where($this->relation)->find();
		
	}
	
	protected function options($item = []) {
		
		$this->assign('work_states',config('base.work_states'));
		
		return $item;
	}
}
